﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.Clients;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.OutboundShipments;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Domain.Storages;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SaleOutRepositoryCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SaleOutRepositoryCommandHandler
{
    public class GetSaleOutDetailCommandHandler : IRequestHandler<GetSaleOutDetailCommand, APIResult<Viw_SaleOutDetail>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BaseStorage> storeBiz;
        private readonly IBaseRepository<Material_BasicModel> meterialModelBiz;
        private readonly IBaseRepository<SaleOutRepository> saleOutBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public GetSaleOutDetailCommandHandler(IMapper mapper, IBaseRepository<BaseStorage> storeBiz, IBaseRepository<Material_BasicModel> meterialModelBiz, IBaseRepository<SaleOutRepository> saleOutBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.storeBiz = storeBiz;
            this.meterialModelBiz = meterialModelBiz;
            this.saleOutBiz = saleOutBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;
        }

        public Task<APIResult<Viw_SaleOutDetail>> Handle(GetSaleOutDetailCommand request, CancellationToken cancellationToken)
        {
            var stores = storeBiz.GetAll().Result;
            var materialModels = meterialModelBiz.GetAll().Result;
            var saleOuts = saleOutBiz.Quary(x => x.Id.ToString() == request.DocumentCode).Result.FirstOrDefault();
            var orderMaterials = orderMaterialsBiz.Quary(x => x.SaleCode.ToString() == request.DocumentCode && x.MaterialsClass == 3).Result.ToList();
            APIResult<Viw_SaleOutDetail> result = new APIResult<Viw_SaleOutDetail>();
            result.data = new Viw_SaleOutDetail
            {
                OrderMaterials = orderMaterials,
                Id = saleOuts.Id,
                BusinessType = saleOuts.BusinessType,
                BusinessStatus = saleOuts.BusinessStatus,
                DocumentType = saleOuts.DocumentType,
                DocumentStatus = saleOuts.DocumentStatus,
                CustomerName = saleOuts.CustomerName,
                CustomerCode = saleOuts.CustomerCode,
                Repository = saleOuts.Repository,
                DocumentDate = saleOuts.DocumentDate,
                SendGoodsNumber = saleOuts.SendGoodsNumber,
                SendGoodsType = saleOuts.SendGoodsType,
                BusinessDemptName = saleOuts.BusinessDemptName,
                BusinessDempt = saleOuts.BusinessDempt,
                BusinessUser = saleOuts.BusinessUser,
                QiJianDate = saleOuts.QiJianDate,
                Remark = saleOuts.Remark,
                ExamUserName = saleOuts.ExamUserName,
            };
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.Count = 1;
            return Task.FromResult(result);
        }
    }
}
